package cn.stylefeng.guns.modular.recy.controller;
/*
 */

import cn.stylefeng.roses.core.base.controller.*;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.*;
import cn.stylefeng.guns.modular.recy.service.*;
import org.springframework.beans.factory.annotation.*;
import cn.stylefeng.roses.core.reqres.response.*;
import cn.stylefeng.guns.modular.recy.entity.*;
import java.io.*;
import cn.stylefeng.guns.core.common.page.*;
import com.baomidou.mybatisplus.core.metadata.*;
import com.baomidou.mybatisplus.core.conditions.*;
import cn.stylefeng.guns.modular.recy.vo.*;
import cn.stylefeng.guns.modular.recy.utils.*;
import cn.hutool.core.bean.*;
import java.util.stream.*;
import cn.stylefeng.guns.modular.recy.warpper.*;
import com.baomidou.mybatisplus.core.conditions.query.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.*;
import java.util.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping({ "/product" })
public class ProductController extends BaseController
{
    private String PREFIX;
    @Autowired
    private ProductService productService;

    @Autowired
    private ProductExchangeService productExchangeService;
    
    public ProductController() {
        this.PREFIX = "/modular/recy/product/";
    }
    
    @RequestMapping({ "" })
    public String index() {
        return this.PREFIX + "product.html";
    }
    
    @RequestMapping({ "/add" })
    public String productAdd() {
        return this.PREFIX + "product_add.html";
    }
    
    @RequestMapping({ "/edit" })
    public String productUpdate(@RequestParam("id") final String id) {
        return this.PREFIX + "product_edit.html";
    }
    
    @RequestMapping({ "/detail/{id}" })
    @ResponseBody
    public ResponseData detail(@PathVariable final String id) {
        final Product product = (Product)this.productService.getById((Serializable)id);
        return (ResponseData)ResponseData.success(product);
    }
    
    @RequestMapping({ "/list" })
    @ResponseBody
    public Object list(final Product productQuery) {
        final QueryWrapper<Product> productQueryWrapper = ControllerUtil.createQuery(productQuery, this.getHttpServletRequest());
        final Page<Product> page = (Page<Product>)LayuiPageFactory.defaultPage();
        final IPage<Product> page2 = (IPage<Product>)this.productService.page((IPage)page, (Wrapper)productQueryWrapper);
        final List<ProductVO> productVOList = EntityBind.bind(page2.getRecords(), ProductVO.class);
        final List<Map<String, Object>> list = productVOList.stream().map(product -> BeanUtil.beanToMap(product)).collect(Collectors.toList());
        page2.setRecords((List)new ProductWrapper(list).wrap());
        return LayuiPageFactory.createPageInfo(page2);
    }
    
    @RequestMapping({ "/addItem" })
    @ResponseBody
    public Object add(final Product product) {
        final HttpServletRequest request = this.getHttpServletRequest();
        final StandardServletMultipartResolver resolver = new StandardServletMultipartResolver();
        final MultipartHttpServletRequest multipartRequest = resolver.resolveMultipart(request);
        final MultipartFile file = multipartRequest.getFile("file");
        try {
            final String name = file.getOriginalFilename();
            final InputStream inputStream = file.getInputStream();
            String fileName = ControllerUtil.saveFile("/upload/brand", inputStream, name);
            product.setPhotos(fileName);

        }
        catch (IOException e) {
            e.printStackTrace();
        }
        product.setCreateTime(new Date());
        this.productService.save(product);
        return ProductController.SUCCESS_TIP;
    }
    
    @RequestMapping({ "/delete" })
    @ResponseBody
    public Object delete(@RequestParam final String id) {
        QueryWrapper<ProductExchange> productExchangeQueryWrapper = new QueryWrapper<>();
        productExchangeQueryWrapper.lambda().eq(ProductExchange::getProductId,id);
        int productExchangeCount = this.productExchangeService.count();
        if(productExchangeCount >0){
            UpdateWrapper<Product> productUpdateWrapper = new UpdateWrapper<>();
            productUpdateWrapper.lambda().eq(Product::getId,id).set(Product::getStatus,DictUtils.me().getId("product_status::down"));
            this.productService.update(productUpdateWrapper);
            return new ErrorResponseData("该商品已经有兑换记录无法删除，已帮您下架！");
        }
        this.productService.removeById((Serializable)id);
        return ProductController.SUCCESS_TIP;
    }
    
    @RequestMapping({ "/editItem" })
    @ResponseBody
    public Object update(final Product product) {
        final HttpServletRequest request = this.getHttpServletRequest();
        final String header = request.getHeader("content-type");
        if (StringUtils.containsIgnoreCase((CharSequence)header, (CharSequence)"multipart/form-data")) {
            final StandardServletMultipartResolver resolver = new StandardServletMultipartResolver();
            final MultipartHttpServletRequest multipartRequest = resolver.resolveMultipart(request);
            final Map<String, MultipartFile> fileMap = (Map<String, MultipartFile>)multipartRequest.getFileMap();
            final boolean file1 = fileMap.containsKey("file");
            if (file1) {
                final MultipartFile file2 = multipartRequest.getFile("file");
                try {
                    final String name = file2.getOriginalFilename();
                    final InputStream inputStream = file2.getInputStream();
                    String fileName = ControllerUtil.saveFile("/upload/brand", inputStream, name);
                    product.setPhotos(fileName);
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        this.productService.updateById(product);
        return ProductController.SUCCESS_TIP;
    }
    
    @RequestMapping(value = { "/delItems" }, method = { RequestMethod.POST })
    @ResponseBody
    public Object delItem(@RequestBody final String[] ids) {
        this.productService.removeByIds((Collection)Arrays.asList(ids));
        for(String id:ids){
            QueryWrapper<ProductExchange> productExchangeQueryWrapper = new QueryWrapper<>();
            productExchangeQueryWrapper.lambda().eq(ProductExchange::getProductId,id);
            int productExchangeCount = this.productExchangeService.count();
            if(productExchangeCount <=0) {
                this.productService.removeById(id);
            }else{
                Product product = this.productService.getById(id);
                product.setStatus(DictUtils.me().getId("product_status::down"));
                this.productService.updateById(product);
            }
        }
        return ProductController.SUCCESS_TIP;
    }
    
    @RequestMapping({ "/show" })
    public Object show(final String id, final Model model) {
        final Product product = (Product)this.productService.getById((Serializable)id);
        final Map map = BeanUtil.beanToMap(product);
        model.addAttribute("product", new ProductWrapper(map).wrap());
        return this.PREFIX + "product_show.html";
    }
}
